package com.lecyon.farm.util;

import com.lecyon.farm.common.constant.CommonConstant;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author Yao Zheng
 */
@Component
public class StringHelper {

    public static String getObjectValue(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String listToString(List<String> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(",");
        }
        String str = sb.substring(0, sb.lastIndexOf(","));
        return str;
    }

    public static List<String> stringToList(String in) {
        if (in == null || in.equals("")) {
            return null;
        }
        String[] array = in.split(",");
        return Arrays.asList(array);
    }

    /**
     * 字符串转换为ASCII码后相加，计算校验码
     * 本系统专用 非通用方法
     *
     * @param value 要计算的字符串
     * @return 校验码
     */
    public static String stringToAscii(String value) {
        Integer ok = 0;
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            ok += (int) chars[i];
        }
        String hex = Integer.toHexString(ok).toUpperCase();
        return hex.substring(hex.length() - 2);
    }

    /**
     * 根据传入的number进行格式化，不足长度左侧补零
     *
     * @param num       要格式化的数
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return 格式化后的字符串
     */
    public synchronized static String numberFormatFillZero(Integer num, int minLength, int maxLength) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMaximumIntegerDigits(maxLength);
        nf.setMinimumIntegerDigits(minLength);
        return nf.format(num);
    }

    /**
     * 将传入字符串的0去除并转换为 Integer类型
     *
     * @param numberString
     * @param replaceStr
     * @return
     */
    public synchronized static Integer numberFormatString2Integer(String numberString, String replaceStr) {
        String result = null;
        StringBuffer regExpress = new StringBuffer();
        if (null != numberString && numberString.length() > 0) {
            if (null == replaceStr) {
                replaceStr = CommonConstant.NUMBER_STRING_REPLACE_STR;
            }
            regExpress.append("^(").append(replaceStr).append("+)");
            if (numberString.contains(replaceStr)) {
                result = numberString.replaceAll(regExpress.toString(), "");
                if (result.length() == 0) {
                    result = replaceStr;
                }
            }
        }
        return Integer.parseInt(result);
    }

    /**
     * 将传入字符串的0去除并转换为 Long 类型
     *
     * @param numberString
     * @param replaceStr
     * @return
     */
    public synchronized static Long numberFormatString2Long(String numberString, String replaceStr) {
        String result = null;
        StringBuffer regExpress = new StringBuffer();
        if (null != numberString && numberString.length() > 0) {
            if (null == replaceStr) {
                replaceStr = CommonConstant.NUMBER_STRING_REPLACE_STR;
            }
            regExpress.append("^(").append(replaceStr).append("+)");
            if (numberString.contains(replaceStr)) {
                result = numberString.replaceAll(regExpress.toString(), "");
                if (result.length() == 0) {
                    result = replaceStr;
                }
            }
        }
        return Long.parseLong(result);
    }

    /**
     * 将传入字符串的0去除并转换为 BigDecimal 类型
     *
     * @param numberString
     * @param replaceStr
     * @return
     */
    public synchronized static BigDecimal numberFormatString2Decimal(String numberString, String replaceStr) {
        String result = null;
        StringBuffer regExpress = new StringBuffer();
        if (null != numberString && numberString.length() > 0) {
            if (null == replaceStr) {
                replaceStr = CommonConstant.NUMBER_STRING_REPLACE_STR;
            }
            regExpress.append("^(").append(replaceStr).append("+)");
            if (numberString.contains(replaceStr)) {
                result = numberString.replaceAll(regExpress.toString(), "");
                if (result.length() == 0) {
                    result = replaceStr;
                }
            }
        }
        return new BigDecimal(result);
    }

    /**
     * 将传入字符串的左侧的0去除并转换为 String 类型
     *
     * @param numberString
     * @param replaceStr
     * @return
     */
    public synchronized static String numberFormatString2Enum(String numberString, String replaceStr) {
        String result = null;
        StringBuffer regExpress = new StringBuffer();
        if (null != numberString && numberString.length() > 0) {
            if (null == replaceStr) {
                replaceStr = CommonConstant.NUMBER_STRING_REPLACE_STR;
            }
            regExpress.append("^(").append(replaceStr).append("+)");
            if (numberString.contains(replaceStr)) {
                result = numberString.replaceAll(regExpress.toString(), "");
                if (result.length() == 0) {
                    result = replaceStr;
                }
            }
        }
        return result;
    }

    /**
     * 判断字符串是不是double型
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (isEmpty(str)) {
            return false;
        }
        String expression = "[0-9]+[.]{0,1}[0-9]*[dD]{0,1}";
        Pattern pattern = Pattern.compile(expression);
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断网络图片是否存在 postUrl 图片地址链接
     */
    public static boolean isImagesTrue(String imageUrl) {
        URL url;
        try {
            url = new URL(imageUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//            httpURLConnection.setRequestMethod("POST");
//            httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            return false;
        }
    }
}
